import type { ImgLoadState } from "@/data/constant/ImgLoadType"
import type { ResSelectType } from "@/data/constant/ResType"

/**
 * 外部传入数据
 */
export type DataExport = {
    imgUrl?: string,
    pointData?: {
        pointId: number,
        scaleX: number,
        scaleY: number,
        pointRes: ResSelectType
    }[]
}
/**
 * 页面绑定xml数据
 */
export type DataWidget = {
    bgImgUrl?: string,
    pointSelectId?: number,
    bgImgInfoNatural?: {
        imgNaturalHeight: number,
        imgNaturalWidth: number,
    },
    bgImgInfoReal?: ImageViewRealInfo,
    pointInfo?: {
        pointDataHandle?: {
            pointId: number,
            scaleX: number,
            scaleY: number,
            pointRes: ResSelectType
        }[]
    }
    bgImgState?: ImgLoadState
}
export type ImageViewNaturalInfo = {
    imgNaturalHeight: number,
    imgNaturalWidth: number,
    imgWidgetHeight: number,
    imgWidgetWidth: number,
}
export type ImageViewRealInfo = {
    imgRealHeight: number,
    imgRealWidth: number,
    imgWidgetHeight: number,
    imgWidgetWidth: number,
    top: number,
    left: number,
}
/**
 * 适配图片（居中）
 * @param imgInfo 
 * @returns 
 */
export function naturalToReal(imgInfo: ImageViewNaturalInfo): ImageViewRealInfo {
    const imgRateWidget = imgInfo.imgWidgetWidth / imgInfo.imgWidgetHeight; //图片控件的宽高比率
    const imgRateNatural = imgInfo.imgNaturalWidth / imgInfo.imgNaturalHeight; //图片原始的宽高比率
    let imgRealHeight: number, imgRealWidth: number, left: number, top: number
    if (imgRateWidget >= imgRateNatural) {
        //高大于宽,宽留白
        imgRealHeight = imgInfo.imgWidgetHeight;
        imgRealWidth = (imgInfo.imgNaturalWidth / imgInfo.imgNaturalHeight) * imgInfo.imgWidgetHeight
        left = (imgInfo.imgWidgetWidth - imgRealWidth) / 2
        top = 0
    } else {
        //宽大于高,高留白
        imgRealHeight = (imgInfo.imgNaturalHeight / imgInfo.imgNaturalWidth) * imgInfo.imgWidgetWidth;
        imgRealWidth = imgInfo.imgWidgetWidth
        left = 0
        top = (imgInfo.imgWidgetHeight - imgRealHeight) / 2
    }
    return {
        imgRealHeight: imgRealHeight,
        imgRealWidth: imgRealWidth,
        imgWidgetHeight: imgInfo.imgWidgetHeight,
        imgWidgetWidth: imgInfo.imgWidgetWidth,
        top: top,
        left: left,
    }
}